package com.zhxs.nfcscan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.zhhd.libdb.PatrolRecord;
import com.zhxs.nfcscan.R;

import java.util.List;

/**
 * 巡更记录适配器
 */
public class PatrolRecordAdapter extends RecyclerView.Adapter<PatrolRecordAdapter.ViewHolder> {

    private Context context;
    private List<PatrolRecord> dataList;
    private OnItemClickListener onItemClickListener;
    private int selectedPosition = -1;

    public interface OnItemClickListener {
        void onItemClick(PatrolRecord record, int position);
    }

    public PatrolRecordAdapter(Context context, List<PatrolRecord> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_patrol_record, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        PatrolRecord record = dataList.get(position);
        
        // 设置计划名称
        holder.tvPlanName.setText(record.getPatrolPlanName() != null ? record.getPatrolPlanName() : "未知计划");
        
        // 设置巡更地点
        holder.tvPointName.setText(record.getPatrolPointName() != null ? record.getPatrolPointName() : "未知地点");
        
        // 设置巡更人员
        if (record.getPatrolPersonName() != null && !record.getPatrolPersonName().isEmpty()) {
            holder.tvPersonName.setText(record.getPatrolPersonName());
        } else {
            holder.tvPersonName.setText("暂未巡更");
        }
        
        // 设置巡更时间
        if (record.getPatrolTime() != null && !record.getPatrolTime().isEmpty()) {
            holder.tvPatrolTime.setText(record.getPatrolTime());
        } else {
            holder.tvPatrolTime.setText("暂未巡更");
        }
        
        // 设置巡更状态
        String statusText = getStatusText(record.getPatrolStatus());
        holder.tvPatrolStatus.setText(statusText);
        
        // 根据状态设置不同的背景色
        int statusColor = getStatusColor(record.getPatrolStatus());
        holder.tvPatrolStatus.setBackgroundColor(statusColor);
        
//        // 设置选中状态背景和标识
//        if (selectedPosition == position) {
//            holder.itemView.setBackground(ContextCompat.getDrawable(context, R.drawable.item_selected_background));
//            holder.ivSelectedIndicator.setVisibility(View.VISIBLE);
//        } else {
//            holder.itemView.setBackground(ContextCompat.getDrawable(context, R.drawable.item_background));
//            holder.ivSelectedIndicator.setVisibility(View.GONE);
//        }
//
//        // 设置点击事件
//        holder.itemView.setOnClickListener(v -> {
//            if (onItemClickListener != null) {
//                onItemClickListener.onItemClick(record, position);
//            }
//        });
    }

    @Override
    public int getItemCount() {
        return dataList != null ? dataList.size() : 0;
    }

    /**
     * 获取状态文本
     */
    private String getStatusText(int status) {
        switch (status) {
            case 0:
                return "未巡";
            case 1:
                return "已巡";
            case 2:
                return "缺巡";
            case 3:
                return "晚巡";
            case 4:
                return "早巡";
            default:
                return "未知";
        }
    }

    /**
     * 获取状态颜色
     */
    private int getStatusColor(int status) {
        switch (status) {
            case 0:
                return context.getResources().getColor(R.color.common_text_hint_color);
            case 1:
                return context.getResources().getColor(R.color.status_patrolled);
            case 2:
                return context.getResources().getColor(R.color.status_missing);
            case 3:
                return context.getResources().getColor(R.color.status_early);
            case 4:
                return context.getResources().getColor(R.color.status_unpatrolled);
            default:
                return context.getResources().getColor(R.color.status_late);
        }
    }

    /**
     * 更新数据
     */
    public void updateData(List<PatrolRecord> newDataList) {
        this.dataList = newDataList;
        notifyDataSetChanged();
    }

    /**
     * 设置选中位置
     */
    public void setSelectedPosition(int position) {
        int oldPosition = selectedPosition;
        selectedPosition = position;
        if (oldPosition != -1) {
            notifyItemChanged(oldPosition);
        }
        if (selectedPosition != -1) {
            notifyItemChanged(selectedPosition);
        }
    }

    /**
     * 清除选中状态
     */
    public void clearSelection() {
        int oldPosition = selectedPosition;
        selectedPosition = -1;
        if (oldPosition != -1) {
            notifyItemChanged(oldPosition);
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvPlanName;
        TextView tvPointName;
        TextView tvPersonName;
        TextView tvPatrolTime;
        TextView tvPatrolStatus;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvPlanName = itemView.findViewById(R.id.tv_plan_name);
            tvPointName = itemView.findViewById(R.id.tv_point_name);
            tvPersonName = itemView.findViewById(R.id.tv_person_name);
            tvPatrolTime = itemView.findViewById(R.id.tv_patrol_time);
            tvPatrolStatus = itemView.findViewById(R.id.tv_patrol_status);
        }
    }
}





